// components/calendar/index.js

var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: null
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    weekList: ['一', '二', '三', '四', '五', '六', '日'], // 周列表
    monthDayList: [], // 当月的日列表
    currentYear: '', // 当前年
    currentMonth: '', // 当前月
    currentDay: '', // 当前日
    today: '', // 今日
    selectedDate: '',
    signedList: [], // 将签到日期格式化
    isDisabledNextMonth: true, // 是否能跳转下一个月
    sign_status: '',
    todayStyle: 'is-today',
    nowTime: new Date().getTime(),
    is_active_start: '',
    is_active_end: ''
  },

  observers: {
    info(val) {
      if(val){
        this.setData({
          is_active_start: val.active_start_timestamp,
          is_active_end: val.active_end_timestamp
        })
        this.initCurrentDate();
      }
    },
    currentMonth() {
      const today = new Date()
      const todayYear = today.getFullYear()
      const todayMonth = today.getMonth() + 1
      if (this.data.currentYear > todayYear || (this.data.currentYear == todayYear && this.data.currentMonth >= todayMonth)) {
        this.setData({
          isDisabledNextMonth: true
        })
      }
    }
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    created: function () {
      // 组件实例刚刚被创建时
    },
    ready: function () {
      // 在组件布局完成后执行
      // this.initCurrentDate();
    },

    moved: function () {
      // 在组件实例被移动到节点树另一个位置时执行
    },
    detached: function () {
      // 组件实例被从页面节点树移除时
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    tapDay(e) {
      let item = e.currentTarget.dataset.item;
      if (this.data.is_active_start > item.time || this.data.is_active_end < item.time) {
        return
      }
      this.setData({
        today: item.date
      })
      item.date = this.formatIosDate(item.date);
      
      this.triggerEvent('postChild', item)
    },
    // 格式化为兼容ios的格式
    formatIosDate(date) {
      return date.replace(/\//g, '-')
    },
    // 格式化时间
    formatTime(date) {
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      const day = date.getDate()

      return [year, month, day].join('/')
    },
    // 十位数补0
    formatZero(number) {
      return number
    },
    // 初始化当前时间
    initCurrentDate(date) {
      let currentDate = date || new Date()
      let currentYear = currentDate.getFullYear()
      let currentMonth = currentDate.getMonth() + 1
      let currentDay = currentDate.getDate()
      // 传的日期为空默认今日
      if (!date) {
        let day,month;
        month = currentMonth < 10?`0${currentMonth}`:currentMonth;
        day = currentDay < 10?`0${currentDay}`:currentDay;
        const today = `${currentYear}-${month}-${day}`;
        this.setData({
          today: today
        })
        console.log(this.data.today)
      }

      this.setData({
        currentYear: currentYear,
        currentMonth: currentMonth,
        currentDay: currentDay
      }, () => {
        this.initMonthDayList()
      })
    },
    // 初始化当前月的日期列表
    initMonthDayList() {
      // 获取当月1号是周几
      let date = new Date(this.data.currentYear, this.data.currentMonth - 1, 1)
      let weekDay = date.getDay()

      // 获取当月的总天数
      let monthFirstDay = new Date(this.data.currentYear, this.data.currentMonth, 0)
      let monthDay = monthFirstDay.getDate()

      let list = []
      if(weekDay == 0){
        for (let index = 0; index < 6; index++) {
          list.push(null)
        }
      }else{
        for (let index = 0; index < weekDay - 1; index++) {
          list.push(null)
        }
      }
      for (let index = 1; index <= monthDay; index++) {
        if (this.data.currentMonth >= 1 && this.data.currentMonth < 10) {
          if (index >= 1 && index < 10) {
            const itemDate = `${this.data.currentYear}-0${this.data.currentMonth}-0${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.data.currentYear}/${this.data.currentMonth}/${index}`).getTime()
            };
            list.push(item);
          } else {
            const itemDate = `${this.data.currentYear}-0${this.data.currentMonth}-${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.data.currentYear}/${this.data.currentMonth}/${index}`).getTime()
            };
            list.push(item);
          }
        } else if (this.data.currentMonth >= 10) {
          if (index >= 1 && index < 10) {
            const itemDate = `${this.data.currentYear}-${this.data.currentMonth}-0${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.data.currentYear}/${this.data.currentMonth}/${index}`).getTime()
            };
            list.push(item);
          } else {
            const itemDate = `${this.data.currentYear}-${this.data.currentMonth}-${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.data.currentYear}/${this.data.currentMonth}/${index}`).getTime()
            };
            list.push(item);
          }
          if (this.currentMonth >= 1 && this.currentMonth < 10) {
            if (index >= 1 && index < 10) {
              const itemDate = `${this.currentYear}/0${this.currentMonth}/0${index}`;
              let item = {
                day: index,
                date: itemDate, // 具体日期
                time: new Date(`${this.currentYear}-${this.currentMonth}-0${index}`).getTime()
              };
              list.push(item);
            } else {
              const itemDate = `${this.currentYear}/0${this.currentMonth}/${index}`;
              let item = {
                day: index,
                date: itemDate, // 具体日期
                time: new Date(`${this.currentYear}-${this.currentMonth}-${index}`).getTime()
              };
              list.push(item);
            }
          } else if (this.currentMonth >= 10) {
            if (index >= 1 && index < 10) {
              const itemDate = `${this.currentYear}/${this.currentMonth}/0${index}`;
              let item = {
                day: index,
                date: itemDate, // 具体日期
                time: new Date(`${this.currentYear}-${this.currentMonth}-${index}`).getTime()
              };
              list.push(item);
            } else {
              const itemDate = `${this.currentYear}/${this.currentMonth}/${index}`;
              let item = {
                day: index,
                date: itemDate, // 具体日期
                time: new Date(`${this.currentYear}-${this.currentMonth}-${index}`).getTime()
              };
              list.push(item);
            }

          }
        }
      }
      this.setData({
        monthDayList: list
      })
      console.log(this.data.monthDayList)
    },
    // 选中当前日期
    handleSelectDate(event) {
      const item = event.currentTarget.dataset.item
      if (item) {
        this.setData({
          selectedDate: item.date
        })
      }
    },
    // 上一月
    handlePreMonth() {
      this.handleToggleMonth(-1)
    },
    // 下一月
    handleNextMonth() {
      console.log('dddddddddddd', this.data.isDisabledNextMonth)
      // if (!this.data.isDisabledNextMonth) {
      this.handleToggleMonth(1)
      // }
    },
    // 切换月
    handleToggleMonth(data) {
      console.log('ddddddddd')
      let month = this.data.currentMonth += data
      let year = this.data.currentYear
      if (month <= 0) {
        month = 12
        year--
      } else if (month > 12) {
        month = 1
        year++
      }
      this.setData({
        currentYear: year,
        currentMonth: month
      }, () => {
        console.log('ddddddddd')
        this.initMonthDayList()
        this.triggerEvent('change', {
          date: `${year}/${month}`
        })
      })
    },
  }
})